package com.mingke.chat.message.logic;

import com.mingke.chat.config.RedisKeyEnum;
import com.mingke.chat.message.EasyChatMessage;
import com.mingke.chat.message.request.EasyChatReportInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author mingke
 * @function TODO
 * @date 2021/12/20
 */
public class EasyChatOfflineLogic implements EasyChatMessageLogic{
    //日志框架
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private RedisTemplate redisTemplate = null;
    /**
     * 执行前准备的方法
     */
    @Override
    public void prepare(EasyChatMessage message) {

    }

    /**
     * 数据存储的方法
     */
    @Override
    public void doStock(EasyChatMessage message) {

    }

    /**
     * 消息分发的方法
     */
    @Override
    public void dispatch(EasyChatMessage message) {

    }

    /**
     * 处理业务逻辑的方法
     *
     * @param message
     */
    @Override
    public void doLogic(EasyChatMessage message) {
        EasyChatReportInfo reportInfo = (EasyChatReportInfo) message;
        log.warn("收到来自: "+reportInfo.getNumber()+"的上报，此用户已断开连接");
        redisTemplate.opsForHash().put(RedisKeyEnum.UserONLine.getKeyName(), reportInfo.getNumber().toString(), false);
    }

    public EasyChatOfflineLogic() {
        this.redisTemplate = (RedisTemplate) EasyChatLogicContext.instance.getContext().getBean("redisTemplate");
    }
}
